package com.mrent.web.controller.front;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.Member;
import com.mrent.entity.Message;
import com.mrent.service.MessageService;
import com.mrent.service.Page;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;


@Controller
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	
	/**
	 * 跳转消息发布页.
	 * 
	 * @return
	 */
	@RequestMapping("/message/publish")
	public String msgPublish(){
		
		return "front/message/publishMsg";
	}
	
	/**
	 * 消息保存.
	 * 
	 * @param response
	 * @return
	 */
	@RequestMapping("/message/save")
	public String msgSave(HttpSession session,Message message,HttpServletResponse response){
		Member member = (Member)session.getAttribute(Constance.SESSION_MEMBER_NAME);
		Map<String,Object> json = new HashMap<String,Object>();
		if(member != null){
			message.setMemberName(member.getMemberName());
			message.setMemberPhoto(member.getMemberPhoto());
			message.setMessageDate(new Date());
			message.setMessageDateLine(System.currentTimeMillis());
			messageService.saveMessage(message);
			json.put("result", "Success");
		}else{
			json.put("result", "Faile");
			json.put("ErrorCode", "-1");
		}
		RenderUtil.renderJson(json, response);
		return null;
	}
	
	/**
	 * 消息列表.
	 * 
	 * @return
	 */
	@RequestMapping("/message/list")
	public String msgList(HttpServletResponse response){
		
		Page pp = new Page();
		pp.setPageSize(Constance.DEFAULT_PAGE_SIZE);
		
		List<Message> list = messageService.listMessages(pp);
		System.out.println("message size:" +list.size());
		RenderUtil.renderJson(list,response);
		
		return null;
	}

}
